package com.musicxapp.modules;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;

import com.alibaba.fastjson.JSONObject;
import com.facebook.react.bridge.JSApplicationIllegalArgumentException;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReadableNativeMap;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.WritableNativeMap;
import com.musicxapp.modules.mywebview.WebActivity;
import com.musicxapp.modules.util.KuwoRequest;
import com.musicxapp.modules.util.NeteaseRequest;
import com.musicxapp.modules.util.QQRequest;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import okhttp3.Response;


public class ToolModule extends ReactContextBaseJavaModule {
    private ReactContext mContext;
    private final String TAG = "ToolModule";

    public ToolModule(ReactApplicationContext reactContext) {
        super(reactContext);
        mContext = reactContext;
    }

    @Override
    public String getName() {
        return "ToolModule";
    }

    @ReactMethod
    public void sendNetease(String url,ReadableMap data, String cookie, Promise promise) {


        ReadableNativeMap rnmap= (ReadableNativeMap) data;
        HashMap map=rnmap.toHashMap();
        try {
            Response response = NeteaseRequest.getRequest(url,map,cookie);


            WritableMap writableMap=new WritableNativeMap();
            writableMap.putString("body",response.body().string());
            if(url.endsWith("/login/cellphone")){
                List<String> headers = response.networkResponse().headers("Set-Cookie");
                String cc="";
                for (String c:headers) {
                    String s = c.split(";")[0];
                    cc=cc+s+";";
                }
                writableMap.putString("cookie",cc);
            }
            promise.resolve(writableMap);
        } catch (IOException e) {
            e.printStackTrace();
            promise.reject(e.getMessage());

        }
    }

    /**
     *
     * @param method 1 : get  2 : post
     * @param url
     * @param data
     * @param cookie
     * @param promise
     */
    @ReactMethod
    public void sendQQ(int method,String url,ReadableMap data, String cookie, Promise promise) {

        ReadableNativeMap rnmap= (ReadableNativeMap) data;
        HashMap map=rnmap.toHashMap();

        try {
            Response response;
            if(method == 1){
                response = QQRequest.getRequest(url,cookie);
            }else {
                response = QQRequest.postRequest(url,map,cookie);
            }

            WritableMap writableMap=new WritableNativeMap();
            writableMap.putString("body",response.body().string());
            writableMap.putString("params", response.header("Referer"));
            promise.resolve(writableMap);
        } catch (IOException e) {
            e.printStackTrace();
            promise.reject(e.getMessage());

        }
    }
    @ReactMethod
    public void sendKW(String url,Promise promise) {

        try {
            Response response = KuwoRequest.getRequest(url);


            WritableMap writableMap=new WritableNativeMap();
            writableMap.putString("body",response.body().string());
            writableMap.putString("url",url);

            promise.resolve(writableMap);
        } catch (IOException e) {
            e.printStackTrace();
            promise.reject(e.getMessage());

        }
    }
    @ReactMethod
    public void toQQWindow(Promise promise) {
        try{
            Activity currentActivity = getCurrentActivity();
            if(null!=currentActivity){
                Intent intent = new Intent(currentActivity, WebActivity.class);
                currentActivity.startActivity(intent);
            }
        }catch(Exception e){
            throw new JSApplicationIllegalArgumentException(
                    "不能打开Activity : "+e.getMessage());

        }
    }

    @ReactMethod
    public void checkQQ(Promise promise) {
        SharedPreferences qq_cookie = getCurrentActivity().getSharedPreferences("qq_cookie", Context.MODE_PRIVATE);
        String cookie = qq_cookie.getString("cookie", null);
        WritableMap writableMap=new WritableNativeMap();
        writableMap.putString("cookie",cookie);
        promise.resolve(writableMap);
    }
    @ReactMethod
    public void checkNetease(Promise promise) {
        SharedPreferences nete_cookie = getCurrentActivity().getSharedPreferences("163_cookie", Context.MODE_PRIVATE);
        String cookie = nete_cookie.getString("cookie", null);
        WritableMap writableMap=new WritableNativeMap();
        writableMap.putString("cookie",cookie);
        promise.resolve(writableMap);
    }
    @ReactMethod
    public void saveNeteaseCookie(String cookie,Promise promise) {
        SharedPreferences nete_cookie = getCurrentActivity().getSharedPreferences("163_cookie", Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = nete_cookie.edit();
        edit.putString("cookie",cookie);
        edit.commit();
        promise.resolve("ok");
    }

    @ReactMethod
    public void asyState(String jsonList,Promise promise) {
        SharedPreferences list_state = getCurrentActivity().getSharedPreferences("list_state", Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = list_state.edit();
        edit.putString("list_state",jsonList);
        edit.apply();
        promise.resolve("ok");
    }
    @ReactMethod
    public void getState(Promise promise) {
        SharedPreferences list_state = getCurrentActivity().getSharedPreferences("list_state", Context.MODE_PRIVATE);
        String state = list_state.getString("list_state", null);
        WritableMap writableMap=new WritableNativeMap();
        writableMap.putString("state",state);
        promise.resolve(writableMap);
    }

    @ReactMethod
    public void clearSp(Promise promise) {
        SharedPreferences list_state = getCurrentActivity().getSharedPreferences("list_state", Context.MODE_PRIVATE);
        SharedPreferences nete_cookie = getCurrentActivity().getSharedPreferences("163_cookie", Context.MODE_PRIVATE);
        SharedPreferences qq_cookie = getCurrentActivity().getSharedPreferences("qq_cookie", Context.MODE_PRIVATE);
        SharedPreferences.Editor edit1 = list_state.edit();
        SharedPreferences.Editor edit2 = nete_cookie.edit();
        SharedPreferences.Editor edit3 = qq_cookie.edit();
        edit2.putString("cookie","");
        edit3.putString("cookie","");
        edit1.putString("list_state","");
        edit1.apply();
        edit2.apply();
        edit3.apply();
        promise.resolve("ok");
    }

    @ReactMethod
    public void addPlayList(String jsonList,Promise promise) {
        SharedPreferences local_list = getCurrentActivity().getSharedPreferences("local_list", Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = local_list.edit();
        edit.putString("list",jsonList);
        edit.commit();
        promise.resolve("ok");
    }
    @ReactMethod
    public void getPlayList(Promise promise) {
        SharedPreferences list_state = getCurrentActivity().getSharedPreferences("local_list", Context.MODE_PRIVATE);
        String playList = list_state.getString("list", null);
        WritableMap writableMap=new WritableNativeMap();
        writableMap.putString("playList",playList);
        promise.resolve(writableMap);
    }
    @ReactMethod
    public void addListSong(String key,String jsonList,Promise promise) {
        SharedPreferences local_list = getCurrentActivity().getSharedPreferences("local_list", Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = local_list.edit();
        edit.putString(key,jsonList);
        edit.commit();
        promise.resolve("ok");
    }
    @ReactMethod
    public void getListSongs(String key,Promise promise) {
        SharedPreferences list_state = getCurrentActivity().getSharedPreferences("local_list", Context.MODE_PRIVATE);
        String songList = list_state.getString(key, null);
        WritableMap writableMap=new WritableNativeMap();
        writableMap.putString("songs",songList);
        promise.resolve(writableMap);
    }
}